package com.holyn.guofang.view.my;

import android.content.Intent;
import android.support.annotation.IdRes;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.holyn.guofang.R;
import com.holyn.guofang.model.sharepreference.UserInfoSharePreference;
import com.holyn.guofang.model.webhttp.HttpConfigure;
import com.holyn.guofang.model.webhttp.wo.DeviceWo;
import com.holyn.guofang.model.webhttp.wo.StatusWo;
import com.holyn.guofang.model.webhttp.wo.UserInfoWo;
import com.holyn.guofang.presenter.BaseView;
import com.holyn.guofang.presenter.my.MyContract;
import com.holyn.guofang.presenter.my.MyPresenter;
import com.holyn.guofang.utils.HtmlUtil;
import com.holyn.guofang.utils.ValidUtil;
import com.holyn.guofang.view.BaseActivity;
import com.holyn.guofang.viewcommon.dialog.UpdateRemarkDialog;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Holyn on 2017/8/9.
 */

public class MyRegisterInfoActivity extends BaseActivity implements BaseView, MyContract.RegisterInfoView{
    private static final int REQUEST_ACTION_POST_USER_INFO = 0x0001;
    private static final int REQUEST_ACTION_POST_UPDATE_USER_INFO = 0x0002;
    private static final int REQUEST_ACTION_POST_UPDATE_DEVICE_REMARK = 0x0003;
    private static final int REQUEST_ACTION_POST_DEVICE_LIST = 0x0004;

    @BindView(R.id.tv_user_name)
    TextView tvUserName;
    @BindView(R.id.tv_register_type)
    TextView tvRegisterType;
    @BindView(R.id.et_real_name)
    EditText etRealName;
    @BindView(R.id.et_address)
    EditText etAddress;
    @BindView(R.id.et_country)
    EditText etCountry;
    @BindView(R.id.et_area)
    EditText etArea;
    @BindView(R.id.et_contact)
    EditText etContact;
    @BindView(R.id.et_contact_tel)
    EditText etContactTel;
    @BindView(R.id.et_contact_mobile)
    EditText etContactMobile;
    @BindView(R.id.et_contact_email)
    EditText etContactEmail;
    @BindView(R.id.btn_update_info)
    Button btnUpdateInfo;

    String name;
    String mobile = "";
    String email = "";
    String address;
    String country;
    String area;
    String contact;
    String contactTel;
    String contactMobile;
    String contactEmail;
    String checkMethod = "";
    String newCheckMethod = "";
    String unBindCode = "";
    String bindCode = "";

    private UserInfoWo userInfoWo;
    private MyContract.Presenter presenter;

    private int curChangeCheckMethodStatus = 0;//0表示手机，1表示email

    private int curClickStatus = 1;//1-点击已授权使用的设备， 2-点击修改主设备别名， 3-点击修改个人信息， 4-点击修改验证方式

    private UpdateRemarkDialog updateRemarkDialog = null;

    private int mainDeviceId = 0;
    private String deviceRemarkNew = "";

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_my_register_info;
    }

    @Override
    protected void onMyCreate() {
        ButterKnife.bind(this);
        showToolbar("注册信息");

        userInfoWo = UserInfoSharePreference.getUserInfo(MyRegisterInfoActivity.this);

        presenter = new MyPresenter(this);
        presenter.addRegisterInfoViewListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        initView();
    }

    private void initView(){
        UserInfoWo.UserInfoBean userInfoBean = userInfoWo.getUserInfo();
        tvUserName.setText(userInfoBean.getUserName());
        if (userInfoBean.getIsMainDevice()){
            tvRegisterType.setText("主号");
        } else if (userInfoBean.getIsMainDevice()){
            tvRegisterType.setText("附属号");
        }

        mobile = userInfoBean.getMobile();
        email = userInfoBean.getEmail();

        etRealName.setText(userInfoBean.getName());
        etAddress.setText(userInfoBean.getAddress());
        etCountry.setText(userInfoBean.getCountry());
        etArea.setText(userInfoBean.getArea());
        etContact.setText(userInfoBean.getContact());
        etContactTel.setText(userInfoBean.getContactTel());
        etContactMobile.setText(userInfoBean.getContactMobile());
        etContactEmail.setText(userInfoBean.getContactEmail());

    }

    @OnClick({R.id.btn_update_info})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_update_info:
                submitPostUpdateUserInfo();
                break;
        }
    }

    @Override
    public void requestStart(Integer requestAction) {
        showLoadingDialog();
    }

    @Override
    public void requestError(Integer requestAction, Throwable e) {
        toastMsg("出现异常");
        closeLoadingDialog();
    }

    @Override
    public void requestComplete(Integer requestAction) {

    }

    @Override
    public void responseSuccess(Integer requestAction, UserInfoWo userInfoWo) {
//        if (userInfoWo.getStatus() == HttpConfigure.STATUS_SUCCESS){
//            if (curClickStatus == 1){//点击已授权使用的设备，跳转页面
//                startActivity(new Intent(MyRegisterInfoActivity.this, MyRegisterInfoSecondaryDeviceListActivity.class));
//            } else if (curClickStatus == 2){
////                showUpdateRemarkDialog();
//                presenter.postDeviceList(REQUEST_ACTION_POST_DEVICE_LIST, 1);//获取主设备的id
//            } else if (curClickStatus == 3){
//                submitPostUpdateUserInfo();
//            } else if (curClickStatus == 4){//点击修改验证方式，跳转页面
//                goUpdateCheckMethodActivity();
//            }
//        } else {
//            toastMsg("获取注册信息失败");
//        }
//        closeLoadingDialog();
    }

    @Override
    public void responseSuccess(Integer requestAction, StatusWo statusWo) {
        if (requestAction == REQUEST_ACTION_POST_UPDATE_DEVICE_REMARK){
//            if (statusWo.getStatus() == HttpConfigure.STATUS_SUCCESS){
//                toastMsg("修改主设备别名成功");
//                userInfoWo.getUserInfo().setDeviceRemark(deviceRemarkNew);
//                UserInfoSharePreference.saveDeviceRemark(MyRegisterInfoActivity.this, deviceRemarkNew);
//            } else {
//                toastMsg("修改主设备别名失败");
//            }
//            closeLoadingDialog();
        } else if (requestAction == REQUEST_ACTION_POST_UPDATE_USER_INFO){
            if (statusWo.getStatus() == HttpConfigure.STATUS_SUCCESS){
                toastMsg("修改个人信息成功");
                postSuccessThenUpdateUserInfoLocal();
            } else {
                toastMsg("修改个人信息失败");
            }
            closeLoadingDialog();
        }
    }

    @Override
    public void responseSuccess(Integer requestAction, DeviceWo deviceWo) {
//        if (deviceWo.getStatus() == HttpConfigure.STATUS_SUCCESS){
//            List<DeviceWo.DataBean> deviceList = deviceWo.getData();
//            if (deviceList == null){
//                toastMsg("获取主设备失败");
//            } else {
//                if (deviceList.size() == 0){
//                    toastMsg("获取主设备失败");
//                } else {
//                    mainDeviceId = deviceList.get(0).getId();
//                    showUpdateRemarkDialog();
//                }
//            }
//        } else {
//            toastMsg("获取设备列表失败");
//        }
//        closeLoadingDialog();
    }

    private void showUpdateRemarkDialog(){
        if (updateRemarkDialog == null){
            updateRemarkDialog = new UpdateRemarkDialog(MyRegisterInfoActivity.this, new UpdateRemarkDialog.OnSureClickListener() {
                @Override
                public void onSureClick(String value) {
                    deviceRemarkNew = value;
                    presenter.postUpdateDeviceRemark(REQUEST_ACTION_POST_UPDATE_DEVICE_REMARK, mainDeviceId, value);
                    updateRemarkDialog.dismiss();
                }
            });
        }
        updateRemarkDialog.show();
    }

    private void submitPostUpdateUserInfo(){
        name = etRealName.getText().toString().trim();
        if (TextUtils.isEmpty(name)){
            toastMsg("真实名字不能为空");
            return;
        }
//        String mobile = "";
//        String email = "";
        address = etAddress.getText().toString().trim();
        country = etCountry.getText().toString().trim();
        area = etArea.getText().toString().trim();
        contact = etContact.getText().toString().trim();
        contactTel = etContactTel.getText().toString().trim();
        contactMobile = etContactMobile.getText().toString().trim();
        if (TextUtils.isEmpty(contactMobile)){
            toastMsg("联系人手机不能为空");
            return;
        } else if (!ValidUtil.isTelephone(contactMobile)){
            toastMsg("请输入正确的电话号码");
            return;
        }
        contactEmail = etContactEmail.getText().toString().trim();
//        if (TextUtils.isEmpty(contactEmail)){
//            toastMsg("E-mail不能为空");
//            return;
//        } else if (!ValidUtil.isEmail(contactEmail)){
//            toastMsg("请输入正确的E-mail");
//            return;
//        }
        checkMethod = userInfoWo.getCheckCode();
//        String newCheckMethod = "";
//        String unBindCode = "";
//        String bindCode = "";

        presenter.postUpdateUserInfo(REQUEST_ACTION_POST_UPDATE_USER_INFO, name, mobile, email, address, country, area, contact, contactTel, contactMobile, contactEmail, checkMethod, newCheckMethod, unBindCode, bindCode);
    }

    private void postSuccessThenUpdateUserInfoLocal(){
        userInfoWo.getUserInfo().setName(name);
        userInfoWo.getUserInfo().setAddress(address);
        userInfoWo.getUserInfo().setCountry(country);
        userInfoWo.getUserInfo().setArea(area);
        userInfoWo.getUserInfo().setContact(contact);
        userInfoWo.getUserInfo().setContactTel(contactTel);
        userInfoWo.getUserInfo().setContactMobile(contactMobile);
        userInfoWo.getUserInfo().setContactEmail(contactEmail);

        UserInfoSharePreference.saveName(MyRegisterInfoActivity.this, name);
        UserInfoSharePreference.saveAddress(MyRegisterInfoActivity.this, address);
        UserInfoSharePreference.saveCountry(MyRegisterInfoActivity.this, country);
        UserInfoSharePreference.saveArea(MyRegisterInfoActivity.this, area);
        UserInfoSharePreference.saveContact(MyRegisterInfoActivity.this, contact);
        UserInfoSharePreference.saveContactTel(MyRegisterInfoActivity.this, contactTel);
        UserInfoSharePreference.saveContactMobile(MyRegisterInfoActivity.this, contactMobile);
        UserInfoSharePreference.saveContactEmail(MyRegisterInfoActivity.this, contactEmail);

    }

//    private void goUpdateCheckMethodActivity(){
//        String checkMethodValue = etChangeCheckMethodValue.getText().toString().trim();
//        if (curChangeCheckMethodStatus == 0){//手机
//            if (TextUtils.isEmpty(checkMethodValue)){
//                toastMsg("请输入更改验证的手机号码");
//                return;
//            } else if (!ValidUtil.isTelephone(checkMethodValue)){
//                toastMsg("更改验证的手机号码格式不正确");
//                return;
//            }
//        } else if (curChangeCheckMethodStatus == 1){//E-mail
//            if (TextUtils.isEmpty(checkMethodValue)){
//                toastMsg("请输入更改验证的电子邮箱");
//                return;
//            } else if (!ValidUtil.isTelephone(checkMethodValue)){
//                toastMsg("更改验证的电子邮箱格式不正确");
//                return;
//            }
//        }
//
//        MyRegisterInfoUpdateCheckMethodActivity.start(this, curChangeCheckMethodStatus + "", checkMethodValue);
//    }
}
